#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
void swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}
void print(int* nums, int size)
{
	for (int i = 0; i < size; i++)
	{
		printf("%d", nums[i]);
	}
	printf("\n");
}
int sub(const void* x, const void* y)
{
	return *(int*)x - *(int*)y;
}
void find1(int* nums,int left,int right)
{
	for (int i = right; i >= left; i--)
	{
		for (int j = right - 1; j >= left; j--)
		{
			if (nums[i] > nums[j])
			{
				swap(&nums[i], &nums[j]);
				qsort(&nums[j + 1], right - j, sizeof(int), sub);
				find1(nums, j + 1, right);
			}
		}
	}

}
void find(int* nums, int size)
{
	int i = size - 1,j=i-1, Judgment =true;
	int  Range = 0;
	for (i = size - 1;i>0;)
	{
		if (Judgment)
			i = size - 1;
		j = i - 1;
		for (j = i - 1; j >= Range; j--)
		{
			Judgment = false;
			if (nums[i] > nums[j])
			{
				swap(&nums[i], &nums[j]);
				Range = j;
				qsort(&nums[j + 1], size - j-1, sizeof(int), sub);
				Judgment = true;
				print(nums, size);
				break;
			}
		}
		if (Judgment == false && j + 1 == Range && i - j == 2 && Range>0)
		{
			Range--;
			i = size - 1;
		}
		else if (Judgment == false && j + 1 == Range && i - j > 1)
			i--;
	}
}
int main()
{
	int nums[] = { 1,2,3};
	find(nums,sizeof(nums)/sizeof(nums[0]));
	return 0;
}